package gov.child.service.impl;

import gov.child.dao.GuideDao;
import gov.child.dao.impl.GuideDaoImpl;
import gov.child.entities.po.EDepartment;
import gov.child.entities.po.EGuide;
import gov.child.entities.vo.*;
import gov.child.service.GuideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/16
 */
@Service("guideService")
public class GuideServiceImpl implements GuideService {

    @Autowired
    private GuideDao guideDao;

    @Override
    public List find(Department department, Boolean online, Byte... obj) {
        EDepartment d = Department.parseTo(department);
        return BeanTransformer.parseFrom(Guide.class, guideDao.find(d, online, obj));
    }

    @Override
    public Guide findById(Integer id) {
        EGuide guide = guideDao.findById(id);
        if (Objects.isNull(guide)) return Guide.EMPTY_GUIDE;
        return Guide.parseFrom(guide);
    }

    @Override
    public List findAll(Page page) {
        List l = guideDao.findAll(page);
        return BeanTransformer.parseFrom(Guide.class, l);
    }

    @Override
    public List findByObj(Byte obj,Page page) {
        List l = guideDao.findByObj(obj);
        return BeanTransformer.parseFrom(Guide.class, l);
    }

    @Override
    public List findByOnline(Byte online,Page page) {
        List l = guideDao.findByOnline(online);
        return BeanTransformer.parseFrom(Guide.class, l);
    }

    @Override
    public List findByDepartment(Department department,Page page) {
        EDepartment d = Department.parseTo(department);
        List l = guideDao.findByDepartment(d);
        return BeanTransformer.parseFrom(Guide.class, l);
    }

    @Override
    public boolean save(Guide guide) {
        EGuide g = Guide.parseTo(guide);
        guideDao.save(g);
        return true;
    }

    @Override
    public boolean update(Guide guide) {
        EGuide g = Guide.parseTo(guide);
        guideDao.update(g);
        return true;
    }

    @Override
    public boolean delete(Guide guide) {
        EGuide g = Guide.parseTo(guide);
        guideDao.delete(g);
        return true;
    }

    @Override
    public Page getPage(int pageSize) {
        long total = guideDao.total();
        return PageUtil.createPage(pageSize, total, 1);
    }
}
